<template>
  <div>
  <input type="file" ref="file" accept="image/*">
  <button @click="upload">上你大爷传</button>
  </div>
</template>

<script>
  import { getUploadToken } from '../../api/getData'

  export default {
    name: 'upload',
    data() {
      return {
        abc: 123
      }
    },
    created() {

    },
    methods: {
      async upload() {

        var qiniu = require('qiniu-js')
        var config = {
          useCdnDomain: true,
          region: qiniu.region.z0     //华东
        }
        var putExtra = {
          fname: this.$refs.file.files[0],
          params: {},
          mimeType: [] || null
        }

        let token = await getUploadToken()
        var observer = {
          next(res){
            //console.log("next")
            // ...
          },
          error(err){
            //console.log("err")
            //console.log(err)
            // ...
          },
          complete(res){
            //console.log("res")
            // ...
          }
        }

        if (token._result) {
          this.token = token.data.token
          this.key = "/img/"+token.data.key
          //把图片上传到 七牛云
          //console.log(this.$refs.file.files[0])
          //var observable = qiniu.upload(this.$refs.file.files[0], this.key, this.token, putExtra, config)
          var observable=qiniu.overrideUpload
          var subscription = observable.subscribe(observer) // 上传开始

        }
      }
    }
  }
</script>

<style scoped>

</style>
